from . import app_rendering
from flask import request, render_template, redirect

from login.login_notify import LoginNotify
from token_mmwl import Token

@app_rendering.route('/login', methods = ["POST", "GET"])

def login():

    token = request.args.get("token")
    if request.method == 'GET':

        msg = request.args.get('msg')
        

        return render_template('login.html', msg = msg, token = token)


    post_data, method = request.form, request.method#请求的参数，请求参数

    user, pass_word = post_data.get('phone'), post_data.get('pass_word')#用户和密码

    login_notify_info = LoginNotify(user, pass_word, method).login_notify#测试登录和密码的返回值

    if login_notify_info['status'] == 1:#增加登陆token值

        company_id = login_notify_info["resultBody"]["user_info"]["id"]

        token = Token().insert_token(company_id)

        login_notify_info["resultBody"]['token'] = token#将获取的token值增加到返回值当中
    
        return redirect(f"/?token={token}")

    msg = login_notify_info['errorMsg']

    return render_template('login.html', msg = msg, token = token)


    